using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CodingDay.Rx.ReactiveControllers;
using CodingDay.Rx.Shared.Domain;
using Holsee.Lib;


namespace CodingDay.Rx.Client.ViewModels
{
    public class DTOViewModel
    {
        private ReactiveFeedController FeedController { get; set; }

        //View Friendly Properties
        public ObservableCollection<DTO> DTOs { get; set; }
        public ObservableCollection<DTO> SampledDTOs { get; set; }

        public DTOViewModel()
        {
            SampledDTOs = new ObservableCollection<DTO>();
            DTOs = new ObservableCollection<DTO>();
            FeedController = new ReactiveFeedController();
        }

        public void Initialize()
        {
            FeedController.Initialize();
            
            StartFeeding();
        }

        public void StartFeeding()
        {
            DTOs.MergeInsert(FeedController.DTOEventStream
                .Select(evt => evt.EventArgs.Data)
                .ObserveOnDispatcher());

            SampledDTOs.MergeInsert(FeedController.DTOEventStream
                                          .Select(evt => evt.EventArgs.Data)
                                          .Sample(TimeSpan.FromSeconds(1))
                                          .ObserveOnDispatcher()
                                          );
        }

        public void StopFeeding()
        {
            FeedController.Stop();
        }
    }
}